//引入express模块
const express = require("express");
//引入数据库连接模块
const pool = require("../pool.js");
//引入公用函数
const xsp_fn = require("../others/xsp_fn");
//创建路由器对象
const mR = express.Router();
//以下为接口部分

//商品模糊查询信息
//接口地址：http://127.0.0.1:3000/commodity/query
//接口方式：get
mR.get("/query", (req, res, next) => {
  console.log(req.query.goods_name);
  pool.query(
    `select * from goods_info where goods_name like '%${req.query.goods_name}%'`,
    [req.query.goods_name],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      console.log(data[0]);
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});

//商品添加接口
//接口地址：http://127.0.0.1:3000/commodity/add
//接口方式：post
mR.post("/add", (req, res, next) => {
  let c_add = xsp_fn.api_add("goods_info");
  pool.query(c_add.sql, [req.body], (err, data) => {
    if (err) {
      res.send(c_add.err);
      next(err);
    } else {
      console.log(data);
      res.send(c_add.success);
    }
  });
});

//查看商品信息
//接口地址：http://127.0.0.1:3000/commodity/comm
//接口方式：get
mR.get("/comm", (req, res, next) => {
  // console.log(req.query.uid);
  pool.query(
    "select * from goods_info where goods_id=?",
    [req.query.goods_id],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});

//查询全部商品
//接口地址：http://127.0.0.1:3000/commodity/quan
//接口方式：get
mR.get("/quan", (req, res, next) => {
  // console.log(req.query.uid);
  pool.query("select * from goods_info", (err, data) => {
    if (err) {
      console.log(err);
      next(err);
      return;
    }
    res.send({ code: 1, msg: "查询成功", data });
  });
});

//删除商品接口
//接口地址：http://127.0.0.1:3000/commodity/delete
//接口方式：delete
mR.delete("/delete", (req, res, next) => {
  let c_delete = `delete from goods_info where goods_id=?`;
  pool.query(c_delete, [req.query.goods_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.changedRows) {
      res.send({ code: 0, msg: "删除失败" });
    } else {
      res.send({ code: 1, msg: "删除成功" });
    }
  });
});

//修改商品接口
//3.1接口地址：http://127.0.0.1:3000/commodity/update
//接口方式：put
mR.put("/update", (req, res, next) => {
  let c_update = `update goods_info set ? where goods_id=?`;
  pool.query(c_update, [req.body, req.body.goods_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.changedRows) {
      res.send({ code: 1, msg: "修改成功" });
    } else {
      res.send({ code: 0, msg: "修改失败" });
    }
  });
});

//商品分页查询接口
//2.1接口地址：http://127.0.0.1:3000/commodity/list?pno=（显示第几页）&pageSize=（一页分几项数据）
mR.get("/list", (req, res, next) => {
  let { pno = 1, pageSize = 10, username = "" } = req.query;

  let start = (pno - 1) * pageSize; //每页开始数
  let wherestr = "";
  if (username != "") {
    wherestr = ` where uname like '%${username}%'`;
  }

  let sql1 = `select * from goods_info ${wherestr}`;
  let sql2 = `select * from goods_info ${wherestr} limit ${start},${pageSize}`;
  pool.query(sql1, (err, data1) => {
    if (err) {
      console.log(err);
      next(err);
      return;
    }
    recordCount = data1.length;
    pageCount = Math.ceil(recordCount / pageSize);
    pool.query(sql2, (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      if (data[0]) {
        res.send({
          code: 1,
          msg: "查询成功",
          pno,
          pageSize,
          pageCount,
          recordCount,
          data,
        });
      } else {
        res.send({ code: 0, msg: "查询失败", data });
      }
    });
  });
});

//商品是否热门查询信息
//接口地址：http://127.0.0.1:3000/commodity/hot
//接口方式：get
mR.get("/hot", (req, res, next) => {
  console.log(req.query.goods_name);
  pool.query(
    `select * from goods_info where is_hot=?`,
    [req.query.is_hot],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      console.log(data[0]);
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});

//商品人气新品查询信息
//接口地址：http://127.0.0.1:3000/commodity/new?is_new=1
//is_new=1 是热门商品  is_new=0 不是热门商品
//接口方式：get
mR.get("/new", (req, res, next) => {
  console.log(req.query.goods_name);
  pool.query(
    `select * from goods_info where is_new=?`,
    [req.query.is_new],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      console.log(data[0]);
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});

//商品前三查询信息
//接口地址：http://127.0.0.1:3000/commodity/san
//接口方式：get
mR.get("/san", (req, res, next) => {
  pool.query(
    `select * from goods_info where goods_id<4`,
    [req.query],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      console.log(data[0]);
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});

//商品分类分页查询接口
//2.1接口地址：http://127.0.0.1:3000/commodity/fllist?sort_id=(商品类别)&pno=(显示第几页)&pageSize=(一页显示几条数据)
mR.get("/fllist", (req, res, next) => {
  let { pno = 1, pageSize = 10 } = req.query;
  let start = (pno - 1) * pageSize;
  let sql1 = `select * from goods_info where sort_id=?`;
  pool.query(sql1, [req.query.sort_id], (err, data1) => {
    if (err) {
      console.log(err);
      next(err);
      return;
    }
    console.log(req.query.goods_name);
    recordCount = data1.length;
    pageCount = Math.ceil(recordCount / pageSize);
    let sql2 = `select * from goods_info where sort_id=${req.query.sort_id} limit ${start},${pageSize}`;

    pool.query(sql2, (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      if (data[0]) {
        res.send({
          code: 1,
          msg: "查询成功",
          pno,
          pageSize,
          pageCount,
          recordCount,
          data,
        });
      } else {
        res.send({ code: 0, msg: "查询失败", data });
      }
    });
  });
});


//商品分类模糊查询分页查询接口
//2.1接口地址：http://127.0.0.1:3000/commodity/fmollist?sort_id=(分类)&goods_name=(商品名称)&pno=(当前页)&pageSize=(显示几条数据)
mR.get("/fmollist", (req, res, next) => {
  let { pno = 1, pageSize = 10 } = req.query;
  let start = (pno - 1) * pageSize;
  let sql1 = `select * from goods_info where sort_id=? && goods_name like '%${req.query.goods_name}%'`;
  pool.query(sql1, [req.query.sort_id,req.query.goods_name], (err, data1) => {
    if (err) {
      console.log(err);
      next(err);
      return;
    }
    recordCount = data1.length;
    pageCount = Math.ceil(recordCount / pageSize);
    let sql2 = `select * from goods_info where sort_id=${req.query.sort_id} && goods_name like '%${req.query.goods_name}%' limit ${start},${pageSize}`;

    pool.query(sql2, (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      if (data[0]) {
        res.send({
          code: 1,
          msg: "查询成功",
          pno,
          pageSize,
          pageCount,
          recordCount,
          data,
        });
      } else {
        res.send({ code: 0, msg: "查询失败", data });
      }
    });
  });
});
//暴露出去
module.exports = mR;
